/*
 * KeyMap.java
 *
 * Created on 21. Januar 2007, 17:53
 *
 * Copyright (c) 2007 Daniel Gronau
 *
 * This file is part of KanjiTori.
 *
 * KanjiTori is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * KanjiTori is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

package kanjitori.preferences;

import com.jme.system.GameSettings;
import java.util.ArrayList;
import java.util.List;

import static kanjitori.gamestate.MainGameState.*;

/**
 *
 * @author Pirx
 */
public class KeyMap  {
    
    private final static String MOUSE_INVERTED = "MouseInverted";
    private final static String KEY = "Key";

    private static GameSettings settings;
    
    public static final String[] KEYS = new String[]{
        "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q",
        "R","S","T","U","V","W","X","Y","Z","1","2","3","4","5","6","7","8",
        "9","0","LeftCtrl","RightCtrl","Alt","Space","Tab",
        "Left","Right","Up","Down","Return"
    };

    public static final int[] KEY_INPUT = new int[]{
        0x1E, 0x30, 0x2E, 0x20, 0x12, 0x21, 0x22, 0x23, 0x17, 0x24, 0x25, 0x26,
        0x32, 0x31, 0x18, 0x19, 0x10, 0x13, 0x1F, 0x14, 0x16, 0x2F, 0x11, 0x2D,
        0x15, 0x2C, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
        0x1D, 0x9D, 0x38, 0x39, 0x0F, 0xCB, 0xCD, 0xC8, 0xD0, 0x1C
    };
    
    public static final String[] ACTIONS = new String[]{
        FORWARD, BACKWARD, STRAFE_LEFT, STRAFE_RIGHT,
        TURN_LEFT, TURN_RIGHT, TURN_UP, TURN_DOWN,
        TELEPORT, DEF_CHANGE
    };
 
    public void setDefaults() {
        bind("W", ACTIONS[0]); //W -> forward
        bind("S", ACTIONS[1]); //S -> backward
        bind("A", ACTIONS[2]); //A -> strafeLeft
        bind("D", ACTIONS[3]); //D -> strafeRight

        bind("Up", ACTIONS[0]); //UP -> forward
        bind("Down", ACTIONS[1]); //DOWN -> backward
        bind("Left", ACTIONS[4]); //LEFT -> turnLeft
        bind("Right", ACTIONS[5]); //RIGHT -> turnRight
        
        bind("1", ACTIONS[8]); //1 -> teleport
        bind("2", ACTIONS[9]); //2 -> defChange
    }

    public static void init(GameSettings settings) {
        KeyMap.settings = settings;
    }
    
    public static void bind(String key, String action) {
        settings.set(KEY + key, action);
    }

    public static void unbind(String key) {
        settings.remove(KEY + key);
    }

    public static void unbindAll() {
        for (String key : KEYS) {
           unbind(key);
        }
    }
    
    public static boolean isMouseInverted() {
        return settings.getBoolean(MOUSE_INVERTED, false);
    }
    
    public static void setMouseInverted(boolean mouseInverted) {
        settings.setBoolean(MOUSE_INVERTED, mouseInverted);
    }
    
    public static String getAction(String key) {
        return settings.get(KEY + key, null);
    } 
   
    public static List<String> getKeys(String action) {
        List<String> list = new ArrayList<String>();
        for (String key : KEYS) {
            if (action.equals(getAction(key))) {
                list.add(key);
            }
        }
        return list;
    }

    public static String getAction(int keyInput) {
        String key = null;
        for (int index = 0; index < KEY_INPUT.length; index++) {
            if (KEY_INPUT[index] == keyInput) {
                key = KEYS[index];
                break;
            }
        }
        return key == null ? null : getAction(key);
    } 
   
    public static int getActionIndex(String action) {
        for (int i = 0; i < ACTIONS.length; i++) {
            if (ACTIONS[i].equals(action)) {
                return i;
            }
        }
        return -1;
    }
    
}
